﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HitchHikersGuideToTheGalaxy.Models
{
    public class AdminRepository : IAdminRepository
    {
        GalaxyDataContext db = new GalaxyDataContext();
       

        /// <summary>
        /// Count all passengers
        /// </summary>
        /// <returns></returns>
        public int CountAllPassengers()
        {
            return (from a in db.Passengers
                   select a).Count();   
        }

        /// <summary>
        /// Count all Tours
        /// </summary>
        /// <returns></returns>
        public int CountAllTours()
        {
            return (from b in db.Tours
                     select b).Count();
        }

        /// <summary>
        /// Find average numbers of passengers travelling
        /// </summary>
        /// <returns></returns>
        public double CountAveragePassengers()
        {
            return CountAllPassengers() / (Double)CountAllTours();
        }

        /// <summary>
        /// Count all tours from somedate to another date
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public int CountAllTours(DateTime fromdate, DateTime todate)
        {
            return (from b in db.Tours
                    where b.Time >= fromdate && b.Time <= todate
                    select b).Count();
        }

        /// <summary>
        /// Finds most common destination town
        /// </summary>
        /// <returns></returns>
        public string FindMostCommonDestination()
        {
            var q = (from p in db.Destinations
                     from d in db.Tours
                     where d.DestinationIdTo == p.Id
                     select p.Name).Max();
            return q;          
        }
        
        /// <summary>
        /// Find most common departure town
        /// </summary>
        /// <returns></returns>
        public string FindMostCommonDeparture()
        {
            var q = (from p in db.Destinations
                     from d in db.Tours
                     where d.DestinationIdFrom == p.Id
                     select p.Name).Max();
            return q;
        }

    }
}
